package com.example.job.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.deng.template.common.jwt.JwtUtil;
import com.deng.template.common.unit.DateUtils;
import com.example.job.MainActivity;
import com.example.job.R;
import com.example.job.databinding.ActivityCheckBinding;

import java.util.Date;

public class CheckActivity extends BaseCompatActivity {

    private ActivityCheckBinding binding;

    private boolean isCheck = false;

    private String timeResult;

    @SuppressLint({"ResourceAsColor", "SetTextI18n", "CommitPrefEdits"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCheckBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);

        SharedPreferences sharedPreferences = getSharedPreferences("deng-check", MODE_PRIVATE);
        String time = sharedPreferences.getString("expiresTime", "1990-01-01 00:00:00");
        isCheck = new Date().compareTo(DateUtils.stringToDate(time, DateUtils.DAY_TIME_FORMAT)) < 0;

        binding.check.setOnClickListener(v -> {
            String password = binding.password.getText().toString();
            if(this.isCheck) {
                timeResult = time;
                toMainActivity();
            }
            Date expiresTime;
            try {
                expiresTime = JwtUtil.getExpiresTime(password, getResources().getString(R.string.secretKey));
            } catch (Exception e){
                binding.result.setText("无效卡密!");
                binding.result.setTextColor(getResources().getColor(R.color.fail));
                return;
            }
            final String expiresTimeStr = DateUtils.dateToString(expiresTime, DateUtils.DAY_TIME_FORMAT);
            String text = "过期时间为:\n" + expiresTimeStr;
            if(new Date().compareTo(expiresTime) < 0){
                //成功解锁
                binding.result.setText("有效卡密，" + text);
                binding.result.setTextColor(getResources().getColor(R.color.success));
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("expiresTime", expiresTimeStr);
                editor.apply();
                isCheck = true;
                timeResult = expiresTimeStr;
            } else {
                binding.result.setText("过期卡密，" + text);
                binding.result.setTextColor(getResources().getColor(R.color.fail));
            }
        });

        binding.enter.setOnClickListener(v ->{
            //已经校验过
            if(isCheck){
                toMainActivity();
            }
            Toast.makeText(this, "请先校验卡密!", Toast.LENGTH_SHORT).show();
        });
    }

    private void toMainActivity(){
        Toast.makeText(this, "校验卡密成功,过期时间：" + timeResult, Toast.LENGTH_LONG).show();
        Intent intent = new Intent();
        intent.setClass(getApplicationContext(), MainActivity.class);
        startActivity(intent);
    }
}